package top.hmtools;

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import top.hmtools.configurations.GMConfiguration;
import top.hmtools.services.BeanCreateService;
import top.hmtools.services.MapperXmlCreateService;

import com.alibaba.druid.pool.DruidDataSourceFactory;

@ComponentScan
@Configuration
public class GMMain {
    
    private String dbUrl = "jdbc:mysql://127.0.0.1:3307/requestlog?useUnicode=true&amp;characterEncoding=utf-8&allowMultiQueries=true";
    private String dbUsername = "root";
    private String dbPwd = "root";
    private String jdbcDriverClass = "com.mysql.jdbc.Driver";
    
    @Bean
    public DataSource druidDataSource() throws Exception{
        DruidDataSourceFactory druidDataSourceFactory = new DruidDataSourceFactory();
        Map<String,String> properties = new HashMap<String,String>();
        properties.put("url", dbUrl);
        properties.put("username", dbUsername);
        properties.put("password", dbPwd);
        properties.put("driverClassName", jdbcDriverClass);
        @SuppressWarnings("static-access")
        DataSource ds = druidDataSourceFactory.createDataSource(properties);
        return ds;
    }

    @SuppressWarnings("resource")
    public static void main(String params[]){
        //注解方式启动spring framework 
          ApplicationContext context =  new AnnotationConfigApplicationContext(GMMain.class);
          
          GMConfiguration gmConfiguration = context.getBean(GMConfiguration.class);
          gmConfiguration.setUseCamelName(true);
          
          //打印所有被注册的bean
          String[] beanNames = context.getBeanDefinitionNames();
          for(String beanname:beanNames){
              System.out.println(beanname);
          }
          
//          MapperXmlCreateService mapperXmlCreateService =  (MapperXmlCreateService)context.getBean("mapperXmlCreateService");
//          String createMapperXml = mapperXmlCreateService.createMapperXml("cn.jhb.","requestlog","requestlog");
//          System.out.println(createMapperXml);
          
          BeanCreateService beanCreateService = context.getBean(BeanCreateService.class);
          String createBean = beanCreateService.createBean("cn.jhb", "requestlog","requestlog");
          System.out.println(createBean);
      }
}
